package com.gperp.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gperp.common.CustomerResCondition;
import com.gperp.controller.LogAspect;
import com.gperp.mapper.AsCustomsMapper;
import com.gperp.mapper.other.CustomerResMapper;
import com.gperp.mapper.other.SystemconfigMapper;
import com.gperp.pojo.AsCustoms;
import com.gperp.pojo.AsCustomsExample;
import com.gperp.pojo.AsSystemconfig;
import com.gperp.pojo.Result;
import com.gperp.service.CustomerResService;

@Service("customerResService")
public class CustomerResServiceImpl implements CustomerResService{
	@Autowired
    private LogAspect aspect;
	@Autowired
	private CustomerResMapper customerResMapper;
	@Autowired
	private AsCustomsMapper asCustomsMapper;
	@Resource
	private SystemconfigMapper systemconfigMapper;
	
	public Result query(int page, int rows, CustomerResCondition condition) {
		if (condition.getCustomname()!=null &&!condition.getCustomname().equals("")) {
			condition.setCustomname("%"+condition.getCustomname()+"%");
		}
		// 设置分页信息
		PageHelper.startPage(page, rows);
		// 执行查询
		List<AsCustoms> list = customerResMapper.queryAll(condition);
		// 取分页结果
		PageInfo<AsCustoms> pageInfo = new PageInfo<>(list);
		Result r = new Result();
		r.setRows(pageInfo.getList());
		r.setPageIndex(page);
		r.setTotal(pageInfo.getTotal());	
		return r;
	}


	@Override
	public AsCustoms queryById(Long id) {
		// TODO Auto-generated method stub
		return asCustomsMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateCustomer(AsCustoms ascustoms) {
		
		return asCustomsMapper.updateByPrimaryKeySelective(ascustoms);
	}


	@Override
	public int updatecustomStatus(Integer id, Integer status) {
		// TODO Auto-generated method stub
		return customerResMapper.updateStatus(id, status);
	}


	@Override
	public AsCustoms insert(AsCustoms as) {
		// TODO Auto-generated method stub
		as.setAgentname(aspect.getUser().getUsername());
		int a =customerResMapper.insertCustoms(as);
		return as;
	}


	@Override
	public List<AsCustoms> queryByCname(String customename) {
		AsCustomsExample example=new AsCustomsExample();
        example.createCriteria().andCustomnameLike("%"+customename+"%");
		return asCustomsMapper.selectByExample(example);
	}


	@Override
	public List<AsSystemconfig> queryCustomtype(int configtype) {
		List<AsSystemconfig> list = systemconfigMapper.queryBynameIs(configtype);
		return list;
	}


	@Override
	public List<AsCustoms> queryByCnameStart(String customname) {
		AsCustomsExample example=new AsCustomsExample();
        example.createCriteria().andCustomnameLike("%"+customname+"%").andCustomstatusEqualTo(1);
		return asCustomsMapper.selectByExample(example);
	}
}
